package com.yunao.web.utils;

import com.auth0.jwt.JWT;
import com.yunao.commons.constant.CookieConstant;
import com.yunao.commons.utils.CookieUtil;
import com.yunao.entity.User;
import com.yunao.service.UserService;
import com.yunao.service.report.Convert;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;

public class TokenUtil {

              public static User getTokenUser(UserService userService) {
//                 String token = getRequest().getHeader("token");// 从 http 请求头中取出 token
                 String token =  getRequest().getParameter("token");
                 String userId = JWT.decode(token).getAudience().get(0);
                 User user = userService.findOne(Convert.getInt(userId));
                 return user;
             }

            public static String getTokenUserId() {
        //                 String token = getRequest().getHeader("token");// 从 http 请求头中取出 token
                String token =  getRequest().getParameter("token");
                String userId = JWT.decode(token).getAudience().get(0);
                return userId;
            }

    public static User getTokenLogin(UserService userService,String token) {
        String userId = JWT.decode(token).getAudience().get(0);
        User user = userService.findOne(Convert.getInt(userId));
        return user;
    }


            public static HttpServletRequest getRequest() {
                 ServletRequestAttributes requestAttributes = (ServletRequestAttributes) RequestContextHolder
                         .getRequestAttributes();
                 return requestAttributes == null ? null : requestAttributes.getRequest();
             }

            /*public static User getUser(UserService userService){
                User userOutput=userService.findOne(Convert.getInt(TokenUtil.getTokenUserId()));
                return userOutput;
            }*/
 }